let {jwtService} = require('./jwtService.js');
let config = require('../config/config.js');

let checkTokenMiddleware = async (request, response, next) => {
    let token = request.headers['authorization'];
    // console.log('token', token)
    if (!token) {
        // console.log('token is null')
        response.error("token is null", config.code.tokenExpire);
    } else {
        // console.log('token is not null')
        let res = await jwtService.verify(token);
        if (res) {
            // console.log('token正常')
            request.userinfo = res;
            next();
        } else {
            response.error("登录失效", config.code.tokenExpire);
        }
        // jwtService.verify(token).then((data) => {
        //     request.user = data;
        //     next();
        // }).catch((err) => {
        //     console.log('校验失败')
        // })
    }
}

exports.checkTokenMiddleware = checkTokenMiddleware
